import json

import openpyxl

from config.config import BASE_URL


def get_excel_data():
    # print('当前路径：',os.getcwd())
    # 读取excel
    workbook = openpyxl.load_workbook('data/data.xlsx')

    # 选择sheet
    worksheet = workbook['Sheet1']

    # 取keys
    keys = [cell.value for cell in worksheet[2]]

    # print(keys)

    # 获取url/params/data/json/headers的index

    key_url_index = keys.index('url')
    key_params_index = keys.index('params')
    key_data_index = keys.index('data')
    key_json_index = keys.index('json')
    key_headers_index = keys.index('headers')
    key_is_run_index = keys.index('is_run')


    # 获取全部测试用例
    data = []
    for row in worksheet.iter_rows(min_row=3, values_only=True):
        # 元组转列表
        row = list(row)
        # print(row)

        if row[key_is_run_index] is not False:

            # 完整路径拼接
            row[key_url_index] = BASE_URL + row[key_url_index]

            # print(row[key_params_index])
            # json转换
            if row[key_params_index] is not None:
                row[key_params_index] = json.loads(row[key_params_index])
            if row[key_data_index] is not None:
                row[key_data_index] = json.loads(row[key_data_index])
            if row[key_json_index] is not None:
                row[key_json_index] = json.loads(row[key_json_index])
            if row[key_headers_index] is not None:
                row[key_headers_index] = json.loads(row[key_headers_index])

            # print(row)

            data.append(dict(zip(keys, row)))

    print('data', data)

    # 关闭excel
    workbook.close()
    return data


# get_excel_data()